package be.nvlooy.moviereminder.entities;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import java.util.Date;
import java.util.List;

import javax.persistence.*;

@NamedQueries({
	@NamedQuery(name = "User.getByUsername",
		query = "SELECT u FROM User AS u WHERE u.username = :username"),

	@NamedQuery(name = "User.getByName",
			query = "SELECT u FROM User AS u " +
					"WHERE LOWER(u.name) LIKE LOWER(:name) OR LOWER(u.firstName) LIKE LOWER(:name)")
})
@Entity
@Table(name="mrm_user", uniqueConstraints=@UniqueConstraint(columnNames={"username"}))
public class User extends BaseEntity {

	@Id
	@GeneratedValue
    @Column(name="user_id")
	private long id;
	
	@Column(name="username", nullable=false)
	private String username;
	
	@Column(nullable=false)
	private String email;
	
	@Column(nullable=false)
	private String password;
	
	@Column(nullable=false)
	private String name;
	
	@Column(nullable=false)
	private String firstName;
	
	@Column(nullable=false)
	private boolean admin;
	
	@Column(nullable=false)
	private boolean active;
	
	@OneToMany(mappedBy="user")
	private List<Collection> collection = new ArrayList<Collection>();

	@Enumerated(EnumType.STRING)
	private LanguageType language;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastLogin;


	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setAndGeneratePassword(String password) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(password.getBytes());
			BigInteger hash = new BigInteger(1, md5.digest());
		    setPassword(hash.toString(16));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public List<Collection> getCollection() {
		return collection;
	}

	public void setCollection(List<Collection> collection) {
		this.collection = collection;
	}

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	public boolean isAdmin() {
		return admin;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isActive() {
		return active;
	}
	
	public String getFullName() {
		return firstName + " " + name;
	}	
	
	public LanguageType getLanguage() {
		return language;
	}

	public void setLanguage(LanguageType language) {
		this.language = language;
	}

	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}

	public Date getLastLogin() {
		return lastLogin;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}
}
